//
//  LCTabBarButton.m
//  LookCoupons
//
//  Created by 明明方 on 2017/9/8.
//  Copyright © 2017年 明明方. All rights reserved.
//

#import "LCTabBarButton.h"

@implementation LCTabBarButton

//取消高亮所做的事
- (void)setHighlighted:(BOOL)highlighted {
    
}

-(instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        
        //设置文字颜色  正常是黑色   选中是橙色
        [self setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [self setTitleColor:[UIColor orangeColor] forState:UIControlStateSelected];
        //图片居中
        self.imageView.contentMode = UIViewContentModeCenter;
        
        //文字居中
        self.titleLabel.textAlignment = NSTextAlignmentCenter;
        
        //设置文字字体
        self.titleLabel.font = [UIFont systemFontOfSize:12];
    }
    return self;
}


////传递UITabBarItem 给tabBarButton  ，给 tabBarButton 赋值
- (void)setItem:(UITabBarItem *)item {
    _item = item;

//    NSLog(@"%@",_item.image);

//    [self observeValueForKeyPath:nil ofObject:nil change:nil context:nil];

    //KVO : 时刻监听一个对象的属性有没有改变
    //给谁添加观察者
    //Observer按钮

//    [item addObserver:self forKeyPath:@"title" options:NSKeyValueObservingOptionNew context:nil];
//
//    [item addObserver:self forKeyPath:@"image" options:NSKeyValueObservingOptionNew context:nil];
//
//    [item addObserver:self forKeyPath:@"selectedImage" options:NSKeyValueObservingOptionNew context:nil];
//
//    [item addObserver:self forKeyPath:@"badgeValue" options:NSKeyValueObservingOptionNew context:nil];


    [self setTitle:_item.title forState:UIControlStateNormal];

    [self setImage:[UIImage imageWithImage:_item.image] forState:UIControlStateNormal];
//
//    [self setImage:[UIImage imageWithImage:_item.selectedImage] forState:UIControlStateSelected];

//    self.badgeView.badgeValue = _item.badgeValue;

}
//
//-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
////    [self setTitle:_item.title forState:UIControlStateNormal];
//
//    [self setImage:_item.image forState:UIControlStateNormal];
//
//    [self setImage:_item.selectedImage forState:UIControlStateSelected];
//
////    self.badgeView.badgeValue = _item.badgeValue;
//
//
//
//}
//
//
//设置内部的布局
- (void)layoutSubviews {
    [super layoutSubviews];
    
//    1,imageView的布局
    CGFloat imageX = self.width/2 - autoX(54)/2;
    CGFloat imageY = self.height/2 - autoX(41)/2 - 5;
    CGFloat imageW = autoX(54);
    CGFloat imageH = autoX(41);
    self.imageView.frame = CGRectMake(imageX, imageY, imageW, imageH);
    
    //title的布局
    CGFloat titleX = 0;
    CGFloat titleY = imageH - 0.1;
    CGFloat titleW = self.bounds.size.width;
    CGFloat titleH = self.bounds.size.height - titleY + 5;
    self.titleLabel.frame = CGRectMake(titleX, titleY, titleW, titleH);
    
    
    // 3.badgeView
    //    self.badgeView.x = self.bounds.size.width - self.badgeView.bounds.size.width - 10;
    //    self.badgeView.y = 0;
    
}

@end
